/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.dao.impl;

import com.ivanbiz.dao.PurchaseOrderItemDAO;
import com.ivanbiz.helper.GlobalConstant;
import com.ivanbiz.helper.HibernateUtil;
import com.ivanbiz.model.Gudang;
import com.ivanbiz.model.GudangItem;
import com.ivanbiz.model.PurchaseOrder;
import com.ivanbiz.model.PurchaseOrderItem;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Administrator
 */
public class PurchaseOrderItemDAOImpl extends GenericDAOImpl implements PurchaseOrderItemDAO {

    @Override
    public String approvePurchaseOrder(String noPO) throws Exception {
        String status = GlobalConstant.EMPTY_STRING;
        try {
            HibernateUtil.beginTransaction();
            Session session = HibernateUtil.getSession();
            Query queryOrder = session.createQuery("from com.ivanbiz.model.PurchaseOrder p where p.nomerPO ='" + noPO + "'");
            PurchaseOrder po = (PurchaseOrder) queryOrder.uniqueResult();
            po.setStatus(GlobalConstant.Y);
            session.update(po);
            Query query = session.createQuery("from com.ivanbiz.model.PurchaseOrderItem p where p.nomerPO ='" + noPO + "'");
            List listPurchaseOrderItem = query.list();
            for (int x = 0; x < listPurchaseOrderItem.size(); x++) {
                PurchaseOrderItem poOrderItem = (PurchaseOrderItem) listPurchaseOrderItem.get(x);
                Query queryGudangItem = session.createQuery("from com.ivanbiz.model.GudangItem p where p.kodeBarcode ='" + poOrderItem.getKodeBarcode() + "'");
                if (queryGudangItem.uniqueResult() == null) {
                    Gudang gudang = (Gudang) session.load(Gudang.class, Long.parseLong("1"));
                    GudangItem gudangItem = new GudangItem();
                    gudangItem.setKodeBarcode(poOrderItem.getKodeBarcode());
                    gudangItem.setNamaItem(poOrderItem.getNamaItem());
                    gudangItem.setDescription(poOrderItem.getDescription());
                    gudangItem.setHargaModal(poOrderItem.getHargaModal());
                    gudangItem.setStock(poOrderItem.getQuantity());
                    gudangItem.setCreatedDate(new Date());
                    gudangItem.setUpdatedDate(new Date());
                    gudangItem.setGudang(gudang);
                    session.save(gudangItem);
                } else {
                    GudangItem gudangItem = (GudangItem) queryGudangItem.uniqueResult();
                    gudangItem.setStock(gudangItem.getStock() + poOrderItem.getQuantity());
                    gudangItem.setHargaModal(poOrderItem.getHargaModal());
                    session.update(gudangItem);
                }
            }

            HibernateUtil.commitTransaction();
            status = "success";
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }

        return status;
    }

    @Override
    public List getDataByLike(String variable1, Object input1, String variable2, Object input2) throws Exception {
        try {
            Session session = HibernateUtil.getSession();
            Query queryOrder = session.createQuery("from com.ivanbiz.model.PurchaseOrderItem poi where poi." + variable1 + " in ( select po." + variable1 + " from com.ivanbiz.model.PurchaseOrder po where po." + variable1 + " like '%" + input1 + "%' and po." + variable2 + " = '" + input2 + "')");
            List list = queryOrder.list();
            return list;
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
    }

    @Override
    public void save(PurchaseOrderItem purchaseOrderItem, PurchaseOrder purchaseOrder) throws Exception {
        try {
            HibernateUtil.beginTransaction();
            Session session = HibernateUtil.getSession();
            session.save(purchaseOrderItem);
            session.update(purchaseOrder);
            HibernateUtil.commitTransaction();
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
    }

    @Override
    public void delete(PurchaseOrderItem purchaseOrderItem, PurchaseOrder purchaseOrder) throws Exception {
        try {
            HibernateUtil.beginTransaction();
            Session session = HibernateUtil.getSession();
            session.delete(purchaseOrderItem);
            session.update(purchaseOrder);
            HibernateUtil.commitTransaction();
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
    }
}
